package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import bd.CriarTabela;
import bean.Tarefa;

public class TarefaDao {

	public static void adicionarTarefa(Tarefa tarefa){

		Session session = CriarTabela.preparaSessao();
		session.save(tarefa);
		session.beginTransaction().commit();
		session.close();	
	}
	
	public static List<Tarefa> retornarTodasAsTarefasPendentes() {
		Session session = CriarTabela.preparaSessao();
		
		Criteria criteria = session.createCriteria(Tarefa.class);
		criteria.add(Restrictions.eq("pendente", true));
		List<Tarefa> tarefas = new ArrayList<>();
		tarefas = criteria.list();
		
		return tarefas;
	}
	
	public static List<Tarefa> retornarTodasAsTarefasFeitas() {
		
		Session session = CriarTabela.preparaSessao();
		
		Criteria criteria = session.createCriteria(Tarefa.class);
		criteria.add(Restrictions.eq("pendente", false));
		List<Tarefa> tarefas = new ArrayList<>();
		tarefas = criteria.list();
		
		return tarefas;
		
	}
	
	public static void deletarTarefa(Tarefa tarefa)
	{
		Session session = CriarTabela.preparaSessao();
		Transaction transaction = session.beginTransaction();
		Tarefa tarefaBd = (Tarefa)session.load(Tarefa.class, tarefa.getId());
		session.delete(tarefaBd);
		transaction.commit();
		session.close();
		
	}
	
	public static Tarefa buscarTarefa(String tarefa)
	{
		Session session = CriarTabela.preparaSessao();
		Transaction transaction = session.beginTransaction();
		Criteria criteria = session.createCriteria(Tarefa.class);
		criteria.add(Restrictions.eq("tarefa", tarefa));
		Tarefa tar = (Tarefa) criteria.uniqueResult();
		
		return tar;
		
	}
	
	public static void modificarTarefa(Tarefa tarefa)
	{
		Session session = CriarTabela.preparaSessao();
		Transaction transaction = session.beginTransaction();
		Tarefa tarefaBd = (Tarefa)session.load(Tarefa.class, tarefa.getId());
		tarefaBd = tarefa;
		session.update(tarefaBd);
		transaction.commit();
		session.close();
		
	}
}